<?php namespace module\button;

use module\WfSite;
use system\model\Button;

class Site extends WfSite
{
	public function AdminLists()
	{
		$data=Db::table('button')->get();
		View::with('data',$data);
		$this->display('lists');
	}

	public function AdminPost()
	{
		$id=Request::get('id');
		if (IS_POST) {
			$model=new Button;
			$model['id']     = $id;
			$model['name']   = Request::post( 'name' );
			$model['data']   = Request::post( 'data' );
			$model['status'] = 0;
			$model->save();
			message( '保存成功', site_url( 'site/lists' ), 'success' );
		}
		if ($id) {
			//编辑
			$field = Db::table( 'button' )->find( $id );
			View::with( 'field', $field );
		}
		$this->display('post');
	}

	//菜单推送
	public function AdminPush()
	{
		$id=Request::get('id');
		$model=new Button;
		$button=$model->find($id);
		$res=Wx::instance('button')->create($button['data']);
		if ($res['errcode']==0) {
			$on=$model->where('status',1)->first();
			if ($on) {
				$on->status=0;
				$on->save();
			}
			$button->status=1;
			$button->save();
			message( '推送成功', site_url( 'site/lists' ), 'message' );
		}else{
			message( '网络原因，请重试', site_url( 'site/lists' ), 'error' );
		}
	}


}